import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import z from "zod";
import { AddMemberSchema, addMember, getUserOrgsWithAllMembers } from "../queries/orgs";

const app = new Hono()
  .get("/", zValidator("query", z.object({ userId: z.string() })), async (c) => {
    const query = c.req.valid("query");
    const data = await getUserOrgsWithAllMembers(query.userId);
    return c.json({ ok: true, data }, 200);
  })
  .post("/", zValidator("json", AddMemberSchema), async (c) => {
    const data = c.req.valid("json");
    const member = await addMember(data);
    if (!member) {
      return c.json({ ok: false, error: "Member add failed" }, 400);
    }
    return c.json({ ok: true, data }, 200);
  });

export default app;
